package handle;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cachedata.NioSocketWrapper;
import enumerate.SocketEvent;
import enumerate.SocketState;

public class DealHandle implements Handle{

	private static final Logger log = LoggerFactory.getLogger(DealHandle.class);
	
	public SocketState process(NioSocketWrapper socketWrapper, SocketEvent event) {
		try {
			switch (event) {
			case OPEN_READ:
				byte[] bytes = new byte[1024];
				
				int num = socketWrapper.read(true, bytes, 0, bytes.length);
				StringBuilder sb = new StringBuilder();
				while (num == bytes.length) {
					sb.append(new String(bytes));
					num = socketWrapper.read(true, bytes, 0, bytes.length);
				}
				sb.append(new String(bytes));
				log.debug(sb.toString());
				byte[] returnData = "this is a test".getBytes();
				socketWrapper.write(true, returnData, 0, returnData.length);
				break;
				
			case OPEN_WRITE:
				byte[] bs = "this is a test".getBytes();
				socketWrapper.write(true, bs, 0, bs.length);
				break;
			case TIMEOUT:
		
				break;
			case DISCONNECT:
				break;
	
			default:
				break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
